﻿#region Copyright(C) 2018 FI Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	    : FI.Net Application Framework
// COMPONENT ID	    : FI.Core
// COMPONENT DESC   : FI.Net Core Foundation
//
// CREATED DATE/BY  : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY :	
// DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using FI.Core.Configuration;
using FI.Core.Properties;
using FI.Core.Utilities;
using System.Configuration;
using System.Linq;

namespace FI.Core.IoC.Configuration
{
    /// <summary>
    /// The configuration setting for "FI.serviceLocation".
    /// </summary>
    [ConfigurationSectionName(Constants.ConfigurationSections.ServiceLocation)]
    public class ServiceLocationSettings : ConfigurationSection
    {
        #region Constants
        private const string DefaultServiceLocatorProperty = "defaultServiceLocator";
        private const string ServiceLocatorsProperty = "serviceLocators";
        private const string ResolvedAssembliesProperty = "resolvedAssemblies";
        #endregion

        #region Properties
        /// <summary>
        /// Gets the default service locator.
        /// </summary>
        /// <value>
        /// The default service locator.
        /// </value>
        [ConfigurationProperty(DefaultServiceLocatorProperty, IsRequired = false, DefaultValue = "")]
        public string DefaultServiceLocator
        {
            get { return (string)base[DefaultServiceLocatorProperty]; }
            set { base[DefaultServiceLocatorProperty] = value; }
        }

        /// <summary>
        /// Gets the service locators.
        /// </summary>
        /// <value>
        /// The service locators.
        /// </value>
        [ConfigurationProperty(ServiceLocatorsProperty, IsRequired = false)]
        public NameTypeConfigurationElementCollection<ServiceLocatorDataBase> ServiceLocators
        {
            get { return (NameTypeConfigurationElementCollection<ServiceLocatorDataBase>)base[ServiceLocatorsProperty]; }
            set { base[ServiceLocatorsProperty] = value; }
        }

        /// <summary>
        /// Gets or sets the resolved assemblies.
        /// </summary>
        /// <value>
        /// The resolved assemblies.
        /// </value>
        [ConfigurationProperty(ResolvedAssembliesProperty, IsRequired = false)]
        public ConfigurationElementCollection<AssemblyConfigurationElement> ResolvedAssemblies
        {
            get { return (ConfigurationElementCollection<AssemblyConfigurationElement>)base[ResolvedAssembliesProperty]; }
            set { base[ResolvedAssembliesProperty] = value; }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Gets the service locator.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns>The service locator.</returns>
        public IServiceLocator GetServiceLocator(string name = null)
        {
            if (string.IsNullOrEmpty(name))
            {
                if (!string.IsNullOrEmpty(this.DefaultServiceLocator))
                {
                    return this.ServiceLocators.GetConfigurationElement(this.DefaultServiceLocator).CreateServiceLocator();
                }
                return null;
            }

            if (this.ServiceLocators.Cast<NameTypeConfigurationElement>().Any(element => element.Name == name))
            {
                return this.ServiceLocators.GetConfigurationElement(name).CreateServiceLocator();
            }

            throw new ConfigurationErrorsException(Resources.ExceptionServiceLocatorNotExists.Format((object)name));
        }
        #endregion
    }
}
